﻿using SearchInFiles.Global;
using System;
using System.ComponentModel;
using System.Xml.Serialization;

namespace SearchInFiles.Lib.Data
{
    [Serializable] public class SearchFileType:INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler? PropertyChanged;
        private bool m_Enable; [XmlAttribute] public bool Enable
        {
            get => m_Enable;
            set {
                if (m_Enable == value) return;
                m_Enable = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Enable)));
                PropertyChanged?.Invoke(Config.Instance, new PropertyChangedEventArgs(nameof(Config.Instance.AllFileTypes)));
            }
        }
        private string? m_TypeExtension; [XmlAttribute]public string TypeExtension
        {
            get => m_TypeExtension ?? string.Empty;
            set 
            {
                if (string.IsNullOrEmpty(m_TypeExtension) == string.IsNullOrEmpty(value) || (!string.IsNullOrEmpty(m_TypeExtension) && m_TypeExtension.Equals(value)))
                    return;
                m_TypeExtension = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(TypeExtension)));
                PropertyChanged?.Invoke(Config.Instance, new PropertyChangedEventArgs(nameof(Config.Instance.AllFileTypes)));
            }
        }

        public bool TypeExtensionEquals(string Value)
        {
            if(string.IsNullOrEmpty(m_TypeExtension))
                throw new NullReferenceException($"{nameof(TypeExtension)},扩展名为空");
            if (string.IsNullOrEmpty(Value))
                return false;
            if(m_TypeExtension.Equals(Value,StringComparison.OrdinalIgnoreCase))
                return true;
            return false;
        }
    }
}
